package com.iot.framework.rabbitmq;

import com.iot.framework.rabbitmq.service.*;
import com.iot.system.service.IDeviceYieldsService;
import com.iot.system.vo.MessageEvent;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 消息监听器
 *
 * @author XiongNeng
 * @version 1.0
 * @since 2018/3/1
 */
@Component
public class Receiver {

    private static final Logger log = LoggerFactory.getLogger(Receiver.class);


    @Resource
    private DeviceStateService deviceStateService;

    @Resource
    private DeviceYieldService deviceYieldService;

    @Resource
    private InfoDumpStorageService infoDumpStorageService;

    @Resource
    private DeviceAlarmService deviceAlarmService;

    @Resource
    private DeviceOperationService deviceOperationService;

    @Resource
    private DeviceParamService deviceParamService;

    @Resource
    private DevicePowerService devicePowerService;

    /**
     * FANOUT广播队列监听一.
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    /*@RabbitListener(queues = {"FANOUT_QUEUE_A"})
    public void on(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.debug("FANOUT_QUEUE_A " + new String(message.getBody()));
    }*/

    /**
     * FANOUT广播队列监听二.
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception   这里异常需要处理
     */
    /*@RabbitListener(queues = {"FANOUT_QUEUE_B"})
    public void t(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        log.debug("FANOUT_QUEUE_B " + new String(message.getBody()));
    }*/


    DeliverCallback deliverCallback = (consumerTag, message) -> {
        System.out.println("message:回调消费成功---需要" + new String(message.getBody()));

    };

    CancelCallback cancelCallback = consumerTag -> {
        System.out.println("消息消费被中断");
    };
    /**
     * 设备开关机状态
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues ="queue_name", containerFactory="rabbitListenerContainerFactory")
    public void deviceState(Message message, Channel channel) throws IOException {
        //channel.basicConsume("queue_name", true, deliverCallback, cancelCallback);
       /* channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);*/
        ///log.debug("DIRECT " + new String(message.getBody()));
        System.out.println("Receiver_deviceYield:设备状态更新" + new String(message.getBody()));
        deviceStateService.deviceState(new String(message.getBody()));
        ///
    }

    @RabbitListener(queues ="yield_queue", containerFactory="rabbitListenerContainerFactory")
    public void deviceYield(Message message, Channel channel) throws IOException {
        //channel.basicConsume("queue_name", true, deliverCallback, cancelCallback);
       /* channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);*/
        ///log.debug("DIRECT " + new String(message.getBody()));
        System.out.println("Receiver_deviceYield:设备产量消息队列" + new String(message.getBody()));
        deviceYieldService.deviceYield( new String(message.getBody()));
        ///
    }

    @RabbitListener(queues ="alarm_queue", containerFactory="rabbitListenerContainerFactory")
    public void deviceAlarm(Message message, Channel channel) throws IOException {
        //channel.basicConsume("queue_name", true, deliverCallback, cancelCallback);
       /* channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);*/
        ///log.debug("DIRECT " + new String(message.getBody()));
        System.out.println("Receiver_deviceAlarm:报警状态消息对列" + new String(message.getBody()));
        if (new String(message.getBody()).equals("1")){
            return;
        }
        deviceAlarmService.deviceAlarm( new String(message.getBody()));
        ///
    }

    @RabbitListener(queues ="operation_queue", containerFactory="rabbitListenerContainerFactory")
    public void deviceOperation(Message message, Channel channel) throws IOException {
        //channel.basicConsume("queue_name", true, deliverCallback, cancelCallback);
       /* channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);*/
        ///log.debug("DIRECT " + new String(message.getBody()));
        System.out.println("Receiver_deviceOperation:设备产量消息队列" + new String(message.getBody()));
        deviceOperationService.deviceOperation( new String(message.getBody()));
        ///
    }

    @RabbitListener(queues ="param_queue", containerFactory="rabbitListenerContainerFactory")
    public void deviceParam(Message message, Channel channel) throws IOException {
        //channel.basicConsume("queue_name", true, deliverCallback, cancelCallback);
       /* channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);*/
        ///log.debug("DIRECT " + new String(message.getBody()));
        System.out.println("param_queue:设备参数消息队列" + new String(message.getBody()));
        deviceParamService.deviceParam( new String(message.getBody()));
        ///
    }

    @RabbitListener(queues ="queue_dong_an_old", containerFactory="rabbitListenerContainerFactory")
    public void queueDongAnOld(Message message, Channel channel) throws IOException {
        //channel.basicConsume("queue_name", true, deliverCallback, cancelCallback);
       /* channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);*/
        ///log.debug("DIRECT " + new String(message.getBody()));
        System.out.println("queue_dong_an_old:设备产量消息队列" + new String(message.getBody()));
        infoDumpStorageService.infoDumpStorage( new String(message.getBody()));
        ///
    }

    @RabbitListener(queues ="queue_power", containerFactory="rabbitListenerContainerFactory")
    public void queuePower(Message message, Channel channel) throws IOException {
        //channel.basicConsume("queue_name", true, deliverCallback, cancelCallback);
       /* channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);*/
        ///log.debug("DIRECT " + new String(message.getBody()));
        System.out.println("queue_power:设备能耗消息队列" + new String(message.getBody()));
        devicePowerService.devicePower( new String(message.getBody()));
        ///
    }
}
